<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Model\Admin;
use Admin\Form\AdminForm;
use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Zend\Session\Container;
use Zend\Json\Json;


class AdminController extends AbstractActionController {

    public $counterTable;

	public function __construct()
    {
		
		
		
    }
	public function kiemtra(){
		$module = "admin";
		$controller = $this->params('controller');
		$action = $this->params('action');
		
		$sessionModule = new Container('module');
		$sessionController = new Container('controller');
		$sessionAction = new Container('action');
		$sessionModule->lastModule = $module;
		$sessionController->lastController = $controller;
		$sessionAction->lastAction = $action;
		
		$sessionUser = new Container('user');
		if(!$sessionUser->tendangnhap){
			return $this->redirect()->toRoute('auth');
		}
		else if($sessionUser->tendangnhap && $sessionUser->quyen != 1){
			return $this->redirect()->toRoute('home');
		}
	}
    public function indexAction() {
		
		$this->kiemtra();
		
		
    }
	public function getdayAction() {
		
		$this->kiemtra();
		
		$select = new Select();
		$counter = $this->getCounterTable()->fetchDay($select->order('id desc')->limit(31));
		
		$table = array();
		$table['cols'] = array(
			/* define your DataTable columns here
			 * each column gets its own array
			 * syntax of the arrays is:
			 * label => column label
			 * type => data type of column (string, number, date, datetime, boolean)
			 */
			// I assumed your first column is a "string" type
			// and your second column is a "number" type
			// but you can change them if they are not
			array('label' => 'Ngày', 'type' => 'string'),
			array('label' => 'Số lần xem trang', 'type' => 'number'),
			array('label' => 'Lượng khách truy cập', 'type' => 'number')
		);
		$rows = array();
		foreach($counter as $r):
			$temp = array();
			// each column needs to have data inserted via the $temp array
			$temp[] = array('v' => $r['ngay']);
			$temp[] = array('v' => (int) $r['soluotxem']); 
			$temp[] = array('v' => (int) $r['luongtruycap']); 
		
			// insert the temp array into $rows
			$rows[] = array('c' => $temp);
		endforeach;
		$table['rows'] = $rows;
		
		return $this->getResponse()->setContent(Json::encode($table));
		//\Zend\Debug\Debug::dump($this->getResponse()->setContent(Json::encode($table)));
    }
	public function getmonthAction() {
		
		$this->kiemtra();
		
		$select = new Select();
		$counter = $this->getCounterTable()->fetchMonth();
		
		$table = array();
		$table['cols'] = array(
			/* define your DataTable columns here
			 * each column gets its own array
			 * syntax of the arrays is:
			 * label => column label
			 * type => data type of column (string, number, date, datetime, boolean)
			 */
			// I assumed your first column is a "string" type
			// and your second column is a "number" type
			// but you can change them if they are not
			array('label' => 'Tháng', 'type' => 'string'),
			array('label' => 'Số lần xem trang', 'type' => 'number'),
			array('label' => 'Lượng khách truy cập', 'type' => 'number')
		);
		$rows = array();
		foreach($counter as $r):
			$temp = array();
			// each column needs to have data inserted via the $temp array
			$temp[] = array('v' => $r['thang']." - ".$r['nam']);
			$temp[] = array('v' => (int) $r['soluotxem']); 
			$temp[] = array('v' => (int) $r['luongtruycap']); 
		
			// insert the temp array into $rows
			$rows[] = array('c' => $temp);
		endforeach;
		$table['rows'] = $rows;
		
		return $this->getResponse()->setContent(Json::encode($table));
		//\Zend\Debug\Debug::dump($this->getResponse()->setContent(Json::encode($table)));
    }
	public function getCounterTable() {
        if (!$this->counterTable) {
            $sm = $this->getServiceLocator();
            $this->counterTable = $sm->get('Admin\Model\CounterTable');
        }
        return $this->counterTable;
    }

    

}
